﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jy123.DBUtility;
using System.Data;
using Jy123.Model;
using System.Data.SqlClient;

namespace Jy123.BLL.Public
{
    public class Pagination
    {
        /// <summary>
        /// 分页存储过程
        /// </summary>
        /// <param name="mp">参数对象</param>
        /// <returns>数据集</returns>
        public static DataSet GetList(MPagination mp)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Tables", SqlDbType.VarChar, 1000),
					new SqlParameter("@PrimaryKey", SqlDbType.VarChar, 100),
					new SqlParameter("@Sort", SqlDbType.VarChar,200),
					new SqlParameter("@CurrentPage", SqlDbType.Int),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@Fields", SqlDbType.VarChar,1000),
					new SqlParameter("@Filter", SqlDbType.NVarChar,2000),
                    new SqlParameter("@Group", SqlDbType.VarChar,1000)
					};
            parameters[0].Value = mp.Tables;
            parameters[1].Value = mp.PrimaryKey;
            parameters[2].Value = mp.Sort;
            parameters[3].Value = mp.CurrentPage;
            parameters[4].Value = mp.PageSize;
            parameters[5].Value = mp.Fields;
            parameters[6].Value = mp.Filter;
            parameters[7].Value = mp.Group;
            return DbHelperSQL.RunProcedure("SP_Public_Pagination", parameters, "ds");
        }
        /// <summary>
        /// 带返回值分页存储过程
        /// </summary>
        /// <param name="mp">参数对象</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public static DataSet GetList(MPagination mp,out List<int?> counts)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Tables", SqlDbType.VarChar, 1000),
					new SqlParameter("@PrimaryKey", SqlDbType.VarChar, 100),
					new SqlParameter("@Sort", SqlDbType.VarChar,200),
					new SqlParameter("@CurrentPage", SqlDbType.Int),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@Fields", SqlDbType.VarChar,1000),
					new SqlParameter("@Filter", SqlDbType.NVarChar,2000),
                    new SqlParameter("@Group", SqlDbType.VarChar,1000),
                    new SqlParameter("@Count",SqlDbType.Int)
					};
            parameters[0].Value = mp.Tables;
            parameters[1].Value = mp.PrimaryKey;
            parameters[2].Value = mp.Sort;
            parameters[3].Value = mp.CurrentPage;
            parameters[4].Value = mp.PageSize;
            parameters[5].Value = mp.Fields;
            parameters[6].Value = mp.Filter;
            parameters[7].Value = mp.Group;
            parameters[8].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("SP_Public_PaginationCount", parameters,"ds");
            counts = new List<int?>();
            counts.Add(Jy123.Common.MakeString.GetInteger(parameters[8].Value.ToString(),0));
            counts.Add((counts[0] - 1) / mp.PageSize + 1);
            return ds;
        }
    }
}
